// 获取价格组参照关系设置默认表格表列
export const getDefaultReferRelativeColumns = ({ renderSelect, renderInputNumber }) => {
  return [
    { title: '组名称', key: 'groupName' },
    { title: '主城市', key: 'mainCity' },
    {
      title: '参照组',
      key: 'mainGroupUuid',
      render: (h, { row, col, index }) => renderSelect(h, row, col.property, index)
    },
    {
      title: '价格比例',
      key: 'rate',
      render: (h, { row, col }) => renderInputNumber(h, row, col.property)
    }
  ];
}

// 获取价格组设置默认表格表列
export const getDefaultGroupColumns = ({ renderInput, renderRadio }) => {
  return [
    {
      title: '设置组',
      key: 'groupNum',
      render: (h, { row, col }) => renderInput(h, row, col.property)
    },
    { title: '组名', key: 'groupName' },
    { title: '省', key: 'provinceName' },
    { title: '主城市', key: 'cityName' },
    { title: '业务区域', key: 'areaName' },
    {
      title: '是否主省',
      key: 'mainCity',
      notShowTip: true,
      width: 160,
      fixed: 'right',
      render: (h, { row, col, index }) => renderRadio(h, row, col.property, index)
    },
  ];
}

// 重载当前行数据 by 是否主省状态
export const reloadRowByMainCityState = (groupSetting, { index, row, val }) => {
  groupSetting.setRowProp(index, 'mainCity', val);
  const isMainCity = val == '1'; // 是主城市
  if (isMainCity) {
    const changedData = getChangedDataByMainCity(groupSetting, row, index)
    groupSetting.setTableData(changedData);
  }
}

// 获取更改是否主省选项后的表格数据
const getChangedDataByMainCity = (groupSetting, row, currentIndex) => {
  const tableData = groupSetting.getTableData();
  tableData.forEach((item, index) => {
    const isSameGroup = item.groupNum == row.groupNum; // 同一设置组
    const isDifferentRow = index !== currentIndex; // 当前循环数据不是所更改是否主省的行数据
    if (isSameGroup) {
      // cityName是省会名称，可以当做groupName的原值使用
      groupSetting.setRowProp(index, 'groupName', row.cityName);
    }
    if (isSameGroup && isDifferentRow) {
      groupSetting.setRowProp(index, 'mainCity', '2');
    }
  });
  return tableData;
}

// 检查价格组设置表格数据是否有修改
export const checkSetGroupDataIsChanged = (originalList, presentList) => {
  const isChanged = presentList.some((item, index) => {
    const isSameGroupNum = item.groupNum !== originalList[index].groupNum;
    const isSameMainCity = item.mainCity !== originalList[index].mainCity;
    return isSameGroupNum || isSameMainCity;
  });
  return isChanged;
}

// 获取初始化后的价格组参照关系设置表格数据
export const getInitedListByGroupSetReferRelative = (list) => {
  return list.map(item => {
    item.mainGroupUuid = item.mainGroupUuid ?? item.uuid;
    item.mainGroupName = item.mainGroupName ?? item.groupName;
    item.rate = item.mainGroupUuid === item.uuid ? 1 : item.rate;
    return item;
  });
}

/*
  method name - 获取各省成组后的表格数据
  description：
  各省成组 -
  *所有可以设置的非主省row(canSetMain === '1' && mainCity === '2')的设置组从上到下累加1
  *所有不可设置的row的mainCity不变
  *点击各省成组的依据 - 主城市名cityName
  *不可编辑的设置组等同于可编辑且是主城市的设置组时，其组名变更为该主城市的组名
*/
export const getNewListByEachGroup = (list) => {
  const canEditMainCityList = list.filter(item => item.canSetMain === '1');
  const notEditMainCityList = list.filter(item => item.canSetMain === '2');
  const canSetMaps = new Map();
  // first - 处理可设置是否主城市的数据
  canEditMainCityList.forEach((item, index) => {
    item.groupNum = (index + 1).toString();
    item.mainCity = '1';
    item.groupName = item.cityName;
    canSetMaps.set(item.groupNum, item);
  });
  // second - 处理不可设置主城市的数据
  const isNullNoEditList = notEditMainCityList.length !== 0;
  isNullNoEditList && notEditMainCityList.forEach(item => {
    if (canSetMaps.has(item.groupNum)) {
      item.groupName = canSetMaps.get(item.groupNum).groupName;
    }
  });
  // ending - 再合并两组数据
  return [...canEditMainCityList, ...notEditMainCityList];
}
